// by Xeno
//#define __DEBUG__
#define THIS_FILE "fn_backpack.sqf"
#include "x_setup.sqf"
private ["_anim","_mag_types","_magazines","_mags_backpack","_muzzles","_p","_primary","_s","_pistol","_base", "_convertFunc", "_magsdetail", "_curPistMag", "_curPistMagClass", "_loaded_mags"];
_p = player;
_id = __pGetVar(GVAR(pbp_id));
if (_id != -9999) then {
	_p removeAction _id;
	__pSetVar [QGVAR(pbp_id), -9999];
};

_convertFunc = {
	_mags_with_ammo = [];
	{
		_mags_with_ammo set [count _mags_with_ammo, [_x, ((_this select 1) select _forEachIndex) call FUNC(extractMagVals)]];
	} forEach (_this select 0);
	__TRACE_1("_convertFunc","_mags_with_ammo");
	_mags_with_ammo
};

_getMuzzleMags = {
	private ["_weapon", "_mags", "_muzzles"];
	PARAMS_1(_weapon);
	if (_weapon == "") exitWith {[]};
	_muzzles = getArray(configFile/"cfgWeapons"/_weapon/"muzzles");
	__TRACE_1("_getMuzzleMags","_muzzles");
	_mags = [];
	{
		__TRACE_1("_getMuzzleMags","_x");
		_newmags = if (_x != "this") then {
			getArray(configFile/"cfgWeapons"/_weapon/_x/"magazines");
		} else {
			getArray(configFile/"cfgWeapons"/_weapon/"magazines");
		};
		__TRACE_1("_getMuzzleMags","_newmags");
		if (count _newmags > 0) then {
			[_mags, _newmags] call FUNC(arrayPushStack2);
		};
	} forEach _muzzles;
	__TRACE_1("_getMuzzleMags","_mags");
	_mags
};

if (count __pGetVar(GVAR(player_backpack)) == 0) then {
	_primary = primaryWeapon _p;
	if (_primary != "") then {
		_mag_types = [_primary] call _getMuzzleMags;
		{
			_mag_types set [_forEachIndex, toUpper _x];
		} forEach _mag_types;
		_cur_weap_p = currentWeapon _p;
		_cur_muz = currentMuzzle _p;
		_loaded_mags = [_primary] call FUNC(getLoadedMags);
		_magazines = magazines _p;
		_magsdetail = magazinesDetail _p;
		_p selectWeapon _cur_weap_p;
		_p selectWeapon _cur_muz;
		__TRACE_1("bp empty","_magazines");
		__TRACE_1("bp empty","_magsdetail");
		
		_mags_backpack = [];
		_mags_backpack_detail = [];
		{
			_tou = toUpper _x;
			if (_tou in _mag_types) then {
				_mags_backpack set [count _mags_backpack, _tou];
				_mags_backpack_detail set [count _mags_backpack_detail, _magsdetail select _forEachIndex];
			};
		} forEach _magazines;
		__TRACE_1("bp empty","_mags_backpack_detail");
		_primary_items = primaryWeaponItems _p;
		_mags_with_ammocount = [_mags_backpack, _mags_backpack_detail] call _convertFunc;
		_loadedmags_with_ammocount = [_loaded_mags select 1, _loaded_mags select 0] call _convertFunc;
		__TRACE_1("bp empty","_mags_with_ammocount");
		__TRACE_1("bp empty","_loadedmags_with_ammocount");
		GVAR(backpack_helper) = [_primary, _mags_with_ammocount, _primary_items, _loadedmags_with_ammocount];
		__TRACE_1("bp empty","d_backpack_helper");
		_pistol = handgunWeapon _p;
		if (_pistol != "") then {
			_curmuzp = currentMuzzle _p;
			_curweapp = currentWeapon _p;
			_p selectWeapon _pistol;
			_curPistMag = currentMagazineDetail _p;
			_curPistMagClass = currentMagazine _p;
			_p selectWeapon _curweapp;
			_p selectWeapon _curmuzp;
			_p removeWeapon _pistol;
		};
		{_p removeMagazine _x} forEach _mags_backpack;
		_p removeWeapon _primary;
		_anim = animationState _p;
		__pSetVar [QGVAR(player_backpack), [_primary, _mags_with_ammocount, _primary_items, _loadedmags_with_ammocount]];
		waitUntil {animationState player != _anim || {!alive player}};
		if (!alive player) exitWith {};
		_p = player;
		_s = format [(localize "STR_DOM_MISSIONSTRING_154"), [_primary,1] call FUNC(GetDisplayName)];
		#ifdef __DEBUG__
		_curdet = magazinesDetail _p;
		__TRACE_1("","_curdet");
		#endif
		if (_pistol != "") then {
			_p addMagazine [_curPistMagClass, _curPistMag call FUNC(extractMagVals)];
			_p addWeapon _pistol;
			_p selectWeapon _pistol;
		};
		if (__pGetVar(GVAR(pbp_id)) == -9999) then {__pSetVar [QGVAR(pbp_id), _p addAction [_s call FUNC(GreyText), {_this spawn FUNC(backpack)},[],-1,false,true,"","!surfaceIsWater (getPosASL _target) && {vehicle _target == _target} && {(getPos _target) select 2 < 2} && {!(_target getVariable 'xr_pluncon')}"]]};
		if (alive player) then {GVAR(backpack_helper) = []};
	};
} else { // switch weapon
	_primary = primaryWeapon _p;
	if (_primary == "") then {
		_loaded_mags = __pGetVar(GVAR(player_backpack)) select 3;
		__TRACE_1("no prim","_loaded_mags");
		if (count _loaded_mags > 0) then {
			{
				_p addMagazine _x;
			} forEach _loaded_mags;
		};
		_p addWeapon (__pGetVar(GVAR(player_backpack)) select 0);
		_bpmagsx = __pGetVar(GVAR(player_backpack)) select 1;
		if (count _bpmagsx > 0) then {
			{_p addMagazine _x} forEach _bpmagsx;
		};
		_bpmagsx = nil;
		{if (_x != "") then {_p removePrimaryWeaponItem _x}} forEach (primaryWeaponItems _p);
		{if (_x != "") then {_p addPrimaryWeaponItem _x}} foreach (__pGetVar(GVAR(player_backpack)) select 2);
		_muzzles = getArray(configFile/"cfgWeapons"/(__pGetVar(GVAR(player_backpack)) select 0)/"muzzles");
		_p selectWeapon (__pGetVar(GVAR(player_backpack)) select 0);
		_p selectWeapon (_muzzles select 0);
		_s = format [(localize "STR_DOM_MISSIONSTRING_155"), [__pGetVar(GVAR(player_backpack)) select 0,1] call FUNC(GetDisplayName)];
		__pSetVar [QGVAR(player_backpack), []];
		_id = __pGetVar(GVAR(pbp_id));
		if (_id != -9999) then {
			_p removeAction _id;
			__pSetVar [QGVAR(pbp_id), -9999];
		};
		__pSetVar [QGVAR(pbp_id), _p addAction [_s call FUNC(GreyText), {_this spawn FUNC(backpack)},[],-1,false,true,"","!surfaceIsWater (getPosASL _target) && {vehicle _target == _target} && {(getPos _target) select 2 < 2} && {!(_target getVariable 'xr_pluncon')}"]];
	} else {
		_mag_types = [_primary] call _getMuzzleMags;
		{
			_mag_types set [_forEachIndex, toUpper _x];
		} forEach _mag_types;
		_cur_weap_p = currentWeapon _p;
		_cur_muz = currentMuzzle _p;
		_loaded_mags = [_primary] call FUNC(getLoadedMags);
		_magazines = magazines _p;
		_magsdetail = magazinesDetail _p;
		_p selectWeapon _cur_weap_p;
		_p selectWeapon _cur_muz;
		__TRACE_1("prim there","_magazines");
		__TRACE_1("prim there","_magsdetail");
		__TRACE_1("prim there","_loaded_mags");
		_mags_backpack = [];
		_mags_backpack_detail = [];
		{
			_tou = toUpper _x;
			if (_tou in _mag_types) then {
				_mags_backpack set [count _mags_backpack, _tou];
				_mags_backpack_detail set [count _mags_backpack_detail, _magsdetail select _forEachIndex];
				__TRACE_1("prim there","_forEachIndex");
				#ifdef __DEBUG__
				_mdfei = _magsdetail select _forEachIndex;
				__TRACE_1("prim there","_mdfei");
				#endif
			};
		} forEach _magazines;
		_primary_items = primaryWeaponItems _p;
		__TRACE_1("prim there","_mags_backpack");
		__TRACE_1("prim there","_magsdetail");
		_mags_with_ammocount = [_mags_backpack, _mags_backpack_detail] call _convertFunc;
		_loadedmags_with_ammocount = [_loaded_mags select 1, _loaded_mags select 0] call _convertFunc;
		__TRACE_1("prim there","_mags_with_ammocount");
		__TRACE_1("prim there","_loadedmags_with_ammocount");
		GVAR(backpack_helper) = [_primary, _mags_with_ammocount, _primary_items, _loadedmags_with_ammocount];
		__TRACE_1("prim there","d_backpack_helper");
		_pistol = handgunWeapon _p;
		__TRACE_1("prim there","_pistol");
		if (_pistol != "") then {
			_curmuzp = currentMuzzle _p;
			_curweapp = currentWeapon _p;
			_p selectWeapon _pistol;
			_curPistMagClass = currentMagazine _p;
			_curPistMag = currentMagazineDetail _p;
			_p selectWeapon _curweapp;
			_p selectWeapon _curmuzp;
			_p removeWeapon _pistol;
		};
		{_p removeMagazine _x} forEach _mags_backpack;
		_p removeWeapon _primary;
		sleep 1;
		_anim = animationState _p;
		waitUntil {animationState player != _anim || {!alive player}};
		if (!alive player) exitWith {};
		_p = player;
		_oloaded_mags = __pGetVar(GVAR(player_backpack)) select 3;
		if (count _oloaded_mags > 0) then {
			{
				_p addMagazine _x;
			} forEach _oloaded_mags;
		};
		if (_pistol != "") then {
			_p addMagazine [_curPistMagClass, _curPistMag call FUNC(extractMagVals)];
			_p addWeapon _pistol;
		};
		_p addWeapon (__pGetVar(GVAR(player_backpack)) select 0);
		_bpmagsx = __pGetVar(GVAR(player_backpack)) select 1;
		if (count _bpmagsx > 0) then {
			{_p addMagazine _x} forEach _bpmagsx;
		};
		_bpmagsx = nil;
		{if (_x != "") then {_p removePrimaryWeaponItem _x}} forEach (primaryWeaponItems _p);
		{if (_x != "") then {_p addPrimaryWeaponItem _x}} foreach (__pGetVar(GVAR(player_backpack)) select 2);
		_muzzles = getArray(configFile/"cfgWeapons"/(__pGetVar(GVAR(player_backpack)) select 0)/"muzzles");
		_p selectWeapon (__pGetVar(GVAR(player_backpack)) select 0);
		_p selectWeapon (_muzzles select 0);
		__pSetVar [QGVAR(player_backpack), [_primary, _mags_with_ammocount, _primary_items, _loadedmags_with_ammocount]];
		_s = format [(localize "STR_DOM_MISSIONSTRING_154"), [_primary,1] call FUNC(GetDisplayName)];
		_id = __pGetVar(GVAR(pbp_id));
		if (_id == -9999) then {__pSetVar [QGVAR(pbp_id), _p addAction [_s call FUNC(GreyText), {_this spawn FUNC(backpack)},[],-1,false,true,"","!surfaceIsWater (getPosASL _target) && {vehicle _target == _target} && {(getPos _target) select 2 < 2} && {!(_target getVariable 'xr_pluncon')}"]]};
		if (alive player) then {GVAR(backpack_helper) = []};
	};
};